#!/usr/bin/env perl

use 5.006001;
use strict;
use warnings;

my $infile = shift or
    die "No input file specified.\n";

my %funcs;

open my $in, $infile
    or die "Cannot open $infile for reading: $!\n";

while (<$in>) {
    if (/^((?:\w+:)?)\@(.*):(\d+)/) {
        my ($prefix, $path, $ln) = ($1, $2, $3);
        my $key = "$path:$ln";
        my $func = $funcs{$key};
        if (defined $func) {
            #warn "Found $key\n";
            print "$func\n";
            next;
        }

        #print "looking for $path:$ln\n";

        my $file;
        if ($path =~ m{lib((?:\/[-\w]+)+)\.lua}) {
           $file = $1;
           $file =~ s/^\///g;
           $file =~ s/\//\./g;
           $file =~ s/-/_/g;

        } elsif ($path =~ m{[-\w]+\.lua}) {
            $file = $&;

        } else {
            $file = $path;
        }

        if ($ln > 0) {
            open my $in, $path
                or die "failed to open $path for reading: $!\n";
            while (<$in>) {
                if ($. == $ln) {
                    if (/function\s+((?:\w+[.:])*\w+)\s*\(/) {
                        $func = $1;

                    } elsif (/(\S+)\s*=\s*function\s*\(/) {
                        $func = $1;

                    } else {
                        $func = $ln;
                    }

                    last;
                }
            }

            close $in;

            if (!defined $func) {
                die "$path:$ln not found.\n";
            }

        } else {
            $func = $ln;
        }

        $funcs{$key} = "$file:$func";

        print "$prefix$file:$func\n";

    } else {
        print $_;
    }
}

close $in;
